OHModules.flickrBox = (function(NameSpace)
{	
	var totalFotos;
	var containerId; 
	var container;
	var next; 
	var prev; 
	var width = 75;
	var height = 75;
	var marginR = 2;
	var totalSteps;
	var currentStep;	
	
	function start(){		
		containerId = document.getElementById('flickr-box-content-photos');
		container = jQuery(containerId);
		
		container.bind('flickr-start',function(event,customData){
			totalFotos = jQuery(this).find('a').length;	
			container.width(Math.round(totalFotos/6)*((width*3)+(marginR*3)));
			totalSteps = Math.round(totalFotos/6);
			currentStep = 1;
			init();
		});	
		
		next = jQuery(document.getElementById('flickr-box-next'));
		prev = jQuery(document.getElementById('flickr-box-prev'));
		container.trigger("flickr-start",this);		
	};
	
	function init(){		
		if(totalFotos <= 6){
			 next.addClass('disabled');
			 prev.addClass('disabled');
		}else{
			checkStep();
			next.bind('click',function(){modules.flickrBox.nextStep();});
			prev.bind('click',function(){modules.flickrBox.prevStep();});			
		}		
	}
	
	function nextStep(){
		
		if(!jQuery(this).hasClass('disabled') && ((currentStep+1) <= totalSteps)){
			++currentStep;
			container.animate({              
          marginLeft: -((currentStep-1)*231)+"px"
    	}, 500);
	
		}
		checkStep();
	}
	
	function prevStep(){
		if(!jQuery(this).hasClass('disabled') && ((currentStep-1) >= 1)){
			--currentStep;		
		 container.animate({              
          marginLeft: -((currentStep-1)*231)+"px"
    	}, 500);
    	
		}
		checkStep();
	}
	function checkStep(){
		if(currentStep == 1){
			prev.addClass('disabled');
		}else if(currentStep < totalSteps){
			next.removeClass('disabled');
			prev.removeClass('disabled');
		}else{
			next.addClass('disabled');
			prev.removeClass('disabled');
		}
	}	
	
	return {
		start: start,
		nextStep:nextStep,
		prevStep:prevStep
	}
	
})(OHModules);
